package com.finalab.yueqiu.dao.redis;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.Transaction;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Component
    public class JedisDAO implements InitializingBean {

        private static final String REDIS_URL = "redis://127.0.0.1:6379";

        private JedisPool jedisPool;

        @Override
        public void afterPropertiesSet() {
            jedisPool = new JedisPool(REDIS_URL);
        }


        private Jedis getConnection()
        {
            Jedis jedis;
            jedis = jedisPool.getResource();
            return jedis;
        }

        // zlist的操作

        public List<Object> getTx(Transaction tx){
            Jedis jedis = getConnection();
            List<Object> list = tx.exec();
            try {
                tx.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return list;
        }

        public Transaction multi(){
            Jedis jedis = getConnection();
            Transaction tx = jedis.multi();
            return tx;
        }

        public Double zscore(String key, String member){
            Jedis jedis = getConnection();
            Double score = jedis.zscore(key, member);
            jedis.close();
            return score;
        }

        public Long zcard(String key){
            Jedis jedis = getConnection();
            Long result = jedis.zcard(key);
            jedis.close();
            return result;
        }

        public List<Integer> zrevrange(String key, int start, int end){
            Jedis jedis = getConnection();
            Set<String> set = jedis.zrevrange(key, start, end);
            List<Integer> ids = new ArrayList<>();
            for (String entityId:
                 set) {
                ids.add(Integer.valueOf(entityId));
            }
            jedis.close();
            return ids;

        }

        public void zremove(String key, String ... member){
            Jedis jedis = getConnection();
            jedis.zrem(key, member);
            jedis.close();
        }

        public void zadd(String key, Double time, String memeber){
            Jedis jedis = getConnection();
            jedis.zadd(key, time, memeber);
            jedis.close();
        }



        public List<String> brpop(int timeout, String key){
            Jedis jedis = getConnection();
            List<String> list;
            list = jedis.brpop(timeout, key);
            jedis.close();
            return list;
        }

        public void lpush(String key, String member){
            Jedis jedis = getConnection();
            jedis.lpush(key, member);
            jedis.close();
        }

        public void srem(String key, String member){
            Jedis jedis = getConnection();
            jedis.srem(key, member);
            jedis.close();
        }

        public Long scard(String key){
            Jedis jedis = getConnection();
            Long count = jedis.scard(key);
            jedis.close();
            return count;
        }


        public void sadd(String key, String member){
            Jedis jedis = getConnection();
             jedis.sadd(key, member);
             jedis.close();
        }

        public String get(String key)
        {
            Jedis jedis = getConnection();
            String value = jedis.get(key);
            jedis.close();
            return value;
        }

        public void del(String key)
        {
            Jedis jedis = getConnection();
            jedis.del(key);
            jedis.close();
        }

        public void setex(String key, int time, String value)
        {
            Jedis jedis = getConnection();
            jedis.setex(key, time, value);
            jedis.close();
        }

        public void set(String key, String value)
        {
            Jedis jedis = getConnection();
            jedis.set(key, value);
            jedis.close();
        }

        public void flushAll(){
            Jedis jedis = getConnection();
            jedis.flushAll();
            jedis.close();
        }
    }

